
#ifndef RTTRANSPORTUDP_H
#define RTTRANSPORTUDP_H

#include "RtTransportBase.h"
#include "RtSocket.h"
#include "RtInetAddr.h"
#include "RtMessageBlock.h"

class CRtUdpEndpoint;

class CRtTransportUdp : public CRtTransportBase  
{
public:
	CRtTransportUdp(
		IRtReactor* pReactor, 
		CRtUdpEndpoint* pEndpoint,
		const CRtInetAddr& aAddrPeer, 
		BOOL bAcceptor);
	virtual ~CRtTransportUdp();

	virtual RtResult SendData(
		CRtMessageBlock& inData,
		BOOL inSendImmediately = TRUE,
		BOOL inDestroyData = FALSE,
		DataPriority inPriority = DP_MEDIUM, 
		BOOL inNeedAck = TRUE);
	virtual RtResult SetOption(DWORD aCommand, LPVOID aArg);
	virtual RtResult GetOption(DWORD aCommand, LPVOID aArg);

	BOOL IsAcceptor() const { return m_bAcceptor; }
	
	void OnReceiveCallback(LPSTR aData, DWORD aLen);

protected:
	virtual RtResult Open_t() { return RT_OK; }
	virtual RtResult Close_t(RtResult aReason);

private:
	CRtUdpEndpoint* m_endpoint;
	CRtInetAddr m_addrPeer;
	BOOL m_bAcceptor;
};

inline void CRtTransportUdp::OnReceiveCallback(LPSTR aData, DWORD aLen)
{
	CRtMessageBlock mbOn(
		aLen, 
		aData, 
		CRtMessageBlock::DONT_DELETE | CRtMessageBlock::WRITE_LOCKED, 
		aLen);
	
	if (m_pSink)
		m_pSink->OnReceive(mbOn, this);
}

#endif // !RTTRANSPORTUDP_H
